const authorityAdmin = (menu, keys) => {
  const result = []
  menu.forEach((item) => {
    if (!item.children) {
      if (keys.includes(item.key)) {
        result.push(item)
      }
    } else {
      const childrenItem = { ...item, children: [] }
      item.children.forEach((child) => {
        if (!child.children) {
          if (keys.includes(child.key)) {
            childrenItem.children.push(child)
          }
        } else {
          const newChildrenItem = { ...child, children: [] }
          child.children.forEach((grandson) => {
            if (keys.includes(grandson.key)) {
              newChildrenItem.children.push(grandson)
            }
          })
          if (newChildrenItem.children.length) childrenItem.children.push(newChildrenItem)
        }
      })
      if (childrenItem.children.length) result.push(childrenItem)
    }
  })
  return result
}

export default authorityAdmin
